package hash表;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

public class Solution454 {
    public static void main(String[] args) {
        int[] arr1 = new int[]{1, 2};
        int[] arr2 = new int[]{-2, -1};
        int[] arr3 = new int[]{-1, 2};
        int[] arr4 = new int[]{0, 2};

        System.out.println(fourSumCount(arr1, arr2, arr3, arr4));
    }

    public static int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {

        HashMap<Integer, Integer> map1 = new HashMap<>();
        for (int i = 0; i < nums1.length; i++) {
            for (int j = 0; j < nums2.length; j++) {
                int temp = nums1[i] + nums2[j];
                if (map1.containsKey(temp)) {
                    map1.put(temp, map1.get(temp) + 1);
                } else {
                    map1.put(temp, 1);
                }
            }
        }
        int result = 0;
        for (int i = 0; i < nums3.length; i++) {
            for (int j = 0; j < nums4.length; j++) {
                int temp = nums3[i] + nums4[j];
                //这里是为了查看前两个数组和符合的次数
                if (map1.containsKey(-temp)) {
                    result += map1.get(-temp);
                }
            }
        }
        return result;
    }

}
